package grape.house.rest.propertytype.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.house.rest.propertytype.form.PropertyTypeListForm;
import grape.house.service.propertytype.query.PropertyTypeListQuery;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.house.rest.propertytype.form.PropertyTypeCreateForm;
import grape.house.rest.propertytype.form.PropertyTypeUpdateForm;
import grape.house.rest.propertytype.form.PropertyTypeListPageForm;
import grape.house.rest.propertytype.vo.PropertyTypeVo;
import grape.house.rest.propertytype.mapper.PropertyTypeWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.house.service.propertytype.po.PropertyType;
import grape.house.service.propertytype.api.IPropertyTypeService;
import java.util.List;
/**
 * <p>
 * 物业类型 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-17
 */
@RestController
@RequestMapping("/house/propertytype")
@Api(tags = "物业类型相关接口")
public class PropertyTypeController extends BaseController<PropertyTypeVo, PropertyType> {

    @Autowired
    private PropertyTypeWebMapper currentWebMapper;
    @Autowired
    private IPropertyTypeService currentService;



     @ApiOperation("添加物业类型")
     @PreAuthorize("hasAuthority('propertytype:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public PropertyTypeVo create(@RequestBody @Valid PropertyTypeCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }

         PropertyType po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询物业类型")
     @PreAuthorize("hasAuthority('propertytype:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public PropertyTypeVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除物业类型")
     @PreAuthorize("hasAuthority('propertytype:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新物业类型")
     @PreAuthorize("hasAuthority('propertytype:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public PropertyTypeVo update(@PathVariable String id,@RequestBody @Valid PropertyTypeUpdateForm uf) {
         PropertyType po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }


    @ApiOperation("不分页查询物业类型")
    @PreAuthorize("hasAuthority('propertytype:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<PropertyTypeVo> list(PropertyTypeListForm listForm) {
        PropertyType po = currentWebMapper.formToPo(listForm);
        PropertyTypeListQuery propertyTypeListQuery = new PropertyTypeListQuery();
        propertyTypeListQuery.setPoQuery(po);
        return super.list(propertyTypeListQuery);
    }

    @ApiOperation("分页查询物业类型")
    @PreAuthorize("hasAuthority('propertytype:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<PropertyTypeVo> listPage(PropertyTypeListPageForm listPageForm) {
         PropertyType po = currentWebMapper.formToPo(listPageForm);
        PropertyTypeListQuery propertyTypeListQuery = new PropertyTypeListQuery();
        propertyTypeListQuery.setPoQuery(po);
         return super.listPage(propertyTypeListQuery,listPageForm);
     }

}
